<style lang="sass" scoped>
    .main{
        margin:30px 0 10px;
        h1{
            font-size:28px;
        }
    }
    .tips{
        font-size:13px;
        color:#f3201b;
        fong-weight:bold;
    }
    .textarea{
        width:1000px;
        height:600px;
        background:#000;
        color:#fff;
        padding-bottom:80px;
    }

</style>

<template>
<div class="container-fluid">
    <div class="main">
        <h1>正式环境发布</h1>
        <hr>
        <select v-model="company" class="form-control w-150 inline_block mr20">
            <option :value="{}">请选择公司</option>
            <option 
                v-Jurisdiction="item.onlneCode" 
                v-for="item in sysList" 
                :value="item">{{ item.companyName }}</option>
        </select>
        <select v-model="system" class="form-control w-250 inline_block mr20">
            <option :value="{}">请选择需要发布的系统</option>
            <option 
                v-Jurisdiction="item.onlneCode" 
                v-for="item in systems" 
                :value="item">{{ item.sysCode +'--'+item.sysName }}</option>
        </select>  
        <button class="btn btn-danger mr10" @click="shellPushForGit('fabu')" :disabled="isDisabled">立即发布</button>
        <button class="btn btn-info mr10" @click="shellPushForGit('beifen')" :disabled="isDisabled">系统备份</button>
        <button class="btn btn-default" @click="shellPushForGit('huanyuan')" :disabled="isDisabled">系统还原</button>
        <button class="btn btn-danger" @click="shellStopGit">终止发布</button>
    </div>
    <div class="tips mb20">备注：发布系统之前请做一次系统备份，备份只能备份上一次的系统代码，请谨慎使用。</div>

    <textarea id="textarea" class="form-control textarea" v-model="showMsg" placeholder="系统发布进程..."></textarea>

</div>
</template>

<script>
    export default {
        components: {
           
        },
        data(){
            return {
                sysList:[],
                company:{},
                systems:[],
                system:{},
                showMsg:'',
                isDisabled:false,
                companyCode:this.$route.query.companyCode,
                sysCode:this.$route.query.sysCode,
                headInfo:util.getStorage('session','headerInfo')?JSON.parse(util.getStorage('session','headerInfo')):{},
                pushResult:'',
                pushTime:'',
                successStr:'',
                isStop:false,
            }
        },
        watch:{
            'company'(){
                this.systems = this.company.systems;
            },
        },
        mounted() {
            this.$nextTick(()=>{
                this.getSystems();
            })
        },
        methods: {
            // 终止发布
            shellStopGit(){
                if(!this.company.companyCode) {popup.alert({title:'请选择需要终止发布的公司!'});return false;}
                if(!this.system.sysCode) {popup.alert({title:'请选择需要终止发布的系统!'}); return false;}

                popup.confirm({title:`确定终止 ${this.system.sysCode} 系统发布吗？`,yes:(()=>{
                    let socket = io.connect(this.system.onlineSocketSrc);
                    this.isStop = true;

                     // 触发
                    socket.emit('shell begin',{
                        getSign:util.getSignData(),
                        system:this.system.sysCode,
                        serverDir:this.system.serverDir,
                        handleType:'stop',
                        stopString:this.system.stopString
                    }); 

                    // 接收数据
                    socket.on('progress number', (data)=> {
                        if( data === 0 ){
                            this.isStop = true;
                            popup.alert({type:'success',title:'终止系统发布成功!'})
                        }else if(data === 1){
                            this.isStop = false;
                            popup.alert({type:'error',title:'终止系统发布失败!'})
                        }else if(data===2){
                            this.isStop = false;
                            popup.alert({type:'error',title:'进程中无此系统标识!'})
                        }
                    });

                })}) 
            },
            // 获得系统列表
            getSystems(){
                util.ajax({
                    url:config.baseApi+'api/getComSysDataList',
                    success:data=>{
                        this.sysList=data.data
                        if(!this.sysList.length || !this.companyCode || !this.sysCode) return;
                        this.sysList.forEach(item=>{
                            if(this.companyCode == item.companyCode){
                                this.company = item;
                                if(item.systems.length){
                                    item.systems.forEach(item1=>{
                                        if(item1.sysCode == this.sysCode){
                                            this.system = item1
                                        }
                                    })
                                };
                            }
                        })
                    }
                })
            },

            //发布
            shellPushForGit(type){
                let str=''
                switch(type){
                    case 'fabu':
                        str='发布'
                        break;
                    case 'beifen':
                        str='备份'
                        break;
                    case 'huanyuan':
                        str='还原'
                        break;        
                }

                if(!this.company.companyCode) {popup.alert({title:`请选择需要${str}系统的公司!`}); return false;}
                if(!this.system.sysCode) {popup.alert({title: `请选择需要${str}的系统!`}); return false;}
                if(!this.system.onlineSocketSrc){popup.alert({title:`系统${str}功能暂未开通，敬请期待!`}); return false;}

                this.isStop = false;

                // 清空
                this.showMsg  = "";
                this.pushTime = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')

                // 确定执行？
                popup.confirm({title:`确定 ${str} ${this.system.sysCode} 系统吗？`,yes:(()=>{

                    let socket = io.connect(this.system.onlineSocketSrc);
                    // 触发
                    socket.emit('shell begin',{
                        getSign:util.getSignData(),
                        onlineDir:this.system.onlineDir,
                        type:type,
                        serverDir:this.system.serverDir,
                        system:this.system.sysCode,
                    });

                    this.isDisabled=true;

                    let textera = document.getElementById("textarea"); 
                    // 接收数据
                    socket.on('progress number', (data)=> {
                        if( data ===  0 ){
                            if(type === 'fabu'){
                                this.successStr = '系统发布成功!'
                                this.pushResult = 0
                                if(this.system.receiveEmail&&!this.isStop){ this.sendEmail() };
                                this.pushSysLogs()
                            };
                            this.isDisabled=false;
                            let strname = `系统${str}成功!`
                            if(type === 'fabu'&&this.isStop) strname = `终止系统发布成功!`;
                            this.showMsg += util.returnBeiZhu(strname)
                        }else if(data ===  1){
                            if(type === 'fabu'){
                                this.successStr='系统发布失败!'
                                this.pushResult = 1
                                if(this.system.receiveEmail&&!this.isStop){ this.sendEmail() };
                                this.pushSysLogs()
                            };
                            this.isDisabled=false;
                            let strname = `系统${str}成功!`
                            if(type === 'fabu'&&this.isStop) strname = `终止系统发布成功!`;
                            this.showMsg += util.returnBeiZhu(strname)
                        }else {
                            this.showMsg += data + '\n';
                        }
                        textera.scrollTop=textera.scrollHeight+'500';  
                    });
                })}) 
            },
            // 发送邮件
            sendEmail(){
                util.ajax({
                    url:config.baseApi+'api/sendEmail',
                    data:{
                        user:this.headInfo.realName,
                        sysName:this.system.sysName,
                        sysCode:this.system.sysCode,
                        receiveEmail:this.system.receiveEmail,
                        time:moment(new Date()).format('YYYY/MM/DD HH:mm:ss'),
                        successStr:this.successStr,
                        systemType:'online'
                    },
                    success:data=>{
                        if(data.data.messageId&&data.data.response){
                            popup.alert({title:'邮件发送成功!'})
                        };
                    }
                })
            },
            // 发布记录
            pushSysLogs(){
                util.ajax({
                    url:config.baseApi+'api/pushSysLogs',
                    data:{
                        pushUser:this.headInfo.realName,
                        pushType:0,
                        pushSystem:`发布：${this.system.sysName}系统`,
                        pushResult:this.pushResult,
                        pushTime:this.pushTime,
                        sysCode:this.system.sysCode,
                        sysName:this.system.sysName,
                        companyCode:this.company.companyCode,
                        companyName:this.company.companyName,
                        systemType:0,
                    },
                    success:data=>{
                        
                    }
                })
            },
        }
    }

</script>






